﻿using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using UIT.ScrumTool.Service.Business;
using UIT.ScrumTool.Service.Interface;

namespace UIT.ScrumTool.Service.Utilities
{
    /// <summary>
    /// 
    /// File cau hinh end point 
    /// </summary>
    public class SerivceWCF
    {
        // Thuoc tinh
        public static ServiceHost host;

        #region RegisterEndPointAddress

        // dang ky dia chi cua dich vu
        public void RegisterEndPointAddress(String strHost)
        {
            Type serviceType = typeof (ControlService);
            host = new ServiceHost(serviceType, new[] {new Uri(strHost)}); //dang ky mot url
            var behavior = new ServiceMetadataBehavior();
            behavior.HttpGetEnabled = true;
            host.Description.Behaviors.Add(behavior);

            // dang ky service
            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            basicHttpBinding.MaxBufferSize = 2147483647;
            basicHttpBinding.MaxReceivedMessageSize = 2147483647;
            basicHttpBinding.MaxBufferPoolSize = 2147483647;

            basicHttpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
            basicHttpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
            basicHttpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
            basicHttpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
            basicHttpBinding.ReaderQuotas.MaxDepth = 2147483647;
            host.AddServiceEndpoint(typeof(IProjectService), basicHttpBinding, "ProjectService");
            host.AddServiceEndpoint(typeof(IDocumentService), basicHttpBinding, "DocumentService");
            host.AddServiceEndpoint(typeof (IUserService), new BasicHttpBinding(), "UserService");
            host.AddServiceEndpoint(typeof(ITaskService), new BasicHttpBinding(), "TaskService");
            host.AddServiceEndpoint(typeof(IProjectUserService), basicHttpBinding, "ProjectUserService");

            //them mex end point
            host.AddServiceEndpoint(typeof (IMetadataExchange), new BasicHttpBinding(), "MEX");
            host.Open();


        }

        #endregion

        #region StopConnectService

        public void StopConnectService()
        {
            try
            {
                if (host != null)
                {
                    host.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }
}